// APP 入口的 JS
const Hapi = require('hapi');
require('env2')('./.env');
const config = require('./config');
const orders = require('./routes/orders');
const shops = require('./routes/shops');
const users = require('./routes/users');
const upload = require('./routes/upload');
const download = require('./routes/download');
const aima = require('./routes/aima');
const wechat = require('./routes/wechat');
const idcard = require('./routes/idcard');
const juhe = require('./routes/juhe-inter');
const weather = require('./routes/weather');

const routesHelloHapi = require('./routes/hello-hapi');
// 引入自定义的 hapi-swagger 插件配置
const pluginHapiSwagger = require('./plugins/hapi-swagger');
//引入分页插件配置
const pluginHapiPagination = require('./plugins/hapi-pagination');
//jwt 插件配置
const hapiAuthJWT2 = require('hapi-auth-jwt2');
const pluginHapiAuthJWT2 = require('./plugins/hapi-auth-jwt2');

const server = new Hapi.Server();

// 配置服务器启动 host 与端口
server.connection({
  port: config.port,
  host: config.host,
});

const io = require("socket.io")(9527,{
    path: "/websocket"
});

io.on("connect",function(socket) {
    socket.emit('news',{
        test: "test socket.io"
    })
    socket.on("response", function(data) {
        console.log(`event response=${JSON.stringify(data)}`);
    })
})

const init = async () => {
  await server.register([
    // 为系统使用 hapi-swagger
    ...pluginHapiSwagger,
    pluginHapiPagination,
    hapiAuthJWT2,
  ]);
  pluginHapiAuthJWT2(server);
  server.route([
    ...routesHelloHapi,
    ...orders,
    ...shops,
    ...users,
    ...upload,
    ...download,
    ...aima,
    ...wechat,
    ...idcard,
    ...juhe,
    ...weather,
  ]);

  // 启动服务
  await server.start();
  console.log(`Server running at: ${server.info.uri}`);
};

init();